package middleware

import (
	"errors"
	"github.com/bgmp/config"
	"github.com/bgmp/handler"
	"github.com/bgmp/model"
	"github.com/gin-gonic/gin"
	"gorm.io/gorm"
)

func DoctorAuth(c *gin.Context) {
	userId := GetUserId(c)
	user := model.User{
		Model: gorm.Model{
			ID: userId,
		},
	}

	err := user.Get(config.DB)
	if err != nil {
		handler.Errorf(c, "no data")
		c.Abort()
		return
	}

	// TODO 暂时注释掉
	//if !user.IsDoctor() {
	//	handler.Errorf(c, "you are not a doctor")
	//	c.Abort()
	//	return
	//}

	c.Set("Doctor", user)
}

func GetDoctor(c *gin.Context) (*model.User, error) {
	v, f := c.Get("Doctor")
	if !f {
		return nil, errors.New("not exists")
	}
	user := v.(model.User)
	return &user, nil
}
